package com.example.chatroom.api;

import com.example.chatroom.model.Friend;
import com.example.chatroom.model.FriendMapper;
import com.example.chatroom.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendAPI {
    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req){
        HttpSession session=req.getSession(false);
        if (session == null) {
            System.out.println("未检测到登录");
            return new ArrayList<Friend>();
        }
        User user=(User) session.getAttribute("user");
        if (user == null) {
            System.out.println("user不存在");
            return new ArrayList<Friend>();
        }
        List<Friend> list=friendMapper.selectFriendList(user.getUserId());
        return list;
    }
}
